package com.hks360.car_treasure.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class ArcSeekbarView extends View implements View.OnClickListener {
    private static final String DEFAULT_COLOR = "#F96040";
    
    private Paint paint;
    private float clickX;
    private float clickY;
    private RectF rellF;
    private Point circlePoint;//圆心
    private Point clickPoint;//点击的点
    private Boolean isDrawCircle = false;
    private RectF rellF1;
    private Point leftcirclePoint;
    private Point rightcirclePoint;
    private Boolean isDrawLR = false;
    private Point newPoint;
    float newX ;
    float newY ;
    private float legalX;
    private float legalY;
    private int degree;
    private Boolean legalLR = false;
    private Boolean legalDrawCircle = false;
    private int min = 16;
    private int max = 30;

    private OnProgressChangedListener listener;

    public void setOnProgressChangedListener(OnProgressChangedListener listener) {
        this.listener = listener;
    }

    public ArcSeekbarView(Context context) {
        super(context,null);
    }

    public ArcSeekbarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        this.min = attrs.getAttributeIntValue("http://schemas.android.com/apk/res-auto", "min", 10);
        this.max = attrs.getAttributeIntValue("http://schemas.android.com/apk/res-auto", "max", 30);
        degree = min;
        paint = new Paint();
        rellF = new RectF(150,50,550,450);//矩形
        rellF1 = new RectF(150,20,550,190);
        circlePoint = new Point(350,250);
        leftcirclePoint = new Point();
        rightcirclePoint = new Point();
        newPoint = new Point();
        clickPoint = new Point();
        paint.setAntiAlias(true);
        clickX = 150 ;
        clickY = 200 ;
        atDegreeSetX();
		setOnClickListener(this);
    }
    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(3);
        paint.setStyle(Style.STROKE);

//			    canvas.drawRect(rellF, paint);

//				canvas.drawRect(rellF1, paint);
        canvas.drawArc(rellF, -25, -130, false, paint);
//				canvas.drawCircle(350, 250, 100, paint);//辅助的小圆
//				canvas.drawCircle(350, 250, 200, paint);//真是的圆
//				canvas.drawCircle(350, 250, 250, paint);//辅助的大圆


        canvas.drawCircle(150, 200, 40, paint);
        leftcirclePoint.x = 150;
        leftcirclePoint.y = 200;
        canvas.drawCircle(550, 200, 40, paint);

        rightcirclePoint.x = 550;
        rightcirclePoint.y =200;
        paint.setTextSize(40);
        paint.setStyle(Style.FILL);
        paint.setStrokeWidth(1);
        canvas.drawText(min + "°", 125, 215, paint);
        canvas.drawText(max + "°", 525, 215, paint);



        if(isDrawCircle){

            if(clickX > 140 && clickX < 165){
                clickX = leftcirclePoint.x;
                clickY = leftcirclePoint.y;
                isDrawCircle = false;
                isDrawLR = true;
                drawLR(canvas);
            }else if(clickX < 565 && clickX > 540){
                clickX = rightcirclePoint.x;
                clickY = rightcirclePoint.y;
                isDrawCircle = false;
                isDrawLR = true;
                drawLR(canvas);
            }else{
                //画圆弧白色的
                paint.setStrokeWidth(3);
                paint.setColor(Color.WHITE);
                paint.setStyle(Style.STROKE);
                canvas.drawArc(rellF, -25, -130, false, paint);
                //画圆
                paint.setStrokeWidth(3);
                paint.setStyle(Style.FILL);
                paint.setColor(Color.parseColor(DEFAULT_COLOR));
                canvas.drawCircle(clickX, getY(clickX), 40, paint);
                legalX = clickX;
                legalY = getY(clickX);
                legalDrawCircle = isDrawCircle;
                isDrawCircle = false;
                //画圆弧红色的
                paint.setStrokeWidth(4);
                paint.setColor(Color.parseColor(DEFAULT_COLOR));
                paint.setStyle(Style.STROKE);
                canvas.drawArc(rellF, 205, getDegreeFromX(clickX), false, paint);
                //  画度数
                paint.setTextSize(40);
                paint.setStyle(Style.FILL);
                paint.setStrokeWidth(1);
                paint.setColor(Color.WHITE);
                canvas.drawText(getTextFromX( clickX) + "°", clickX - 30, getY(clickX) + 10, paint);
            }
        }else if(isDrawLR){
            drawLR(canvas);
        }
    }

    private void drawLR(Canvas canvas) {
        paint.setStrokeWidth(3);
        paint.setColor(Color.WHITE);
        paint.setStyle(Style.STROKE);
        canvas.drawArc(rellF, -25, -130, false, paint);


        paint.setStrokeWidth(1);
        paint.setStyle(Style.FILL);
        paint.setColor(Color.parseColor(DEFAULT_COLOR));
        canvas.drawCircle(clickX, clickY, 40, paint);

        legalX = clickX;
        legalY = clickY;
        legalLR = isDrawLR;
        isDrawLR = false;

        paint.setStrokeWidth(4);
        paint.setColor(Color.parseColor(DEFAULT_COLOR));
        paint.setStyle(Style.STROKE);
        canvas.drawArc(rellF, 205, getDegreeFromX(clickX), false, paint);


        paint.setTextSize(40);
        paint.setStyle(Style.FILL);
        paint.setStrokeWidth(1);
        paint.setColor(Color.WHITE);
        canvas.drawText(getTextFromX( clickX) + "°", clickX - 25, getY(clickX) - 35, paint);
    }

    public void atDegreeSetX(){
        clickX = 150 + (degree - min) * (max - min);
        isDrawCircle = true;
    }
    public int getTextFromX(float x){
        int y ;
        y = (int) ((x - 150) / (400 / (max - min))) + min;
        return y;
    }
    public int getDegreeFromX(float x){
        int y;
        y = (int) ((getTextFromX(x) - min)*(130.0 / (max - min))) ;

        return y;
    }
    public Boolean isLegal(){
        if(rellF1.contains(newX,newY) && !isContainCircle(newPoint, circlePoint, 100)
                && isContainCircle(newPoint, circlePoint, 250)){
            isDrawCircle = true;
            return true;
        }else if(isContainCircle(newPoint, leftcirclePoint, 40)){
            clickX = leftcirclePoint.x;
            clickY = leftcirclePoint.y;
            isDrawLR = true;
            return true;

        }else if(isContainCircle(newPoint, rightcirclePoint, 40)){
            clickX = rightcirclePoint.x;
            clickY = rightcirclePoint.y;
            isDrawLR = true;
            return true;
        }
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //第一次按下的坐标
                clickX = event.getX();
                clickY = event.getY();
                break;

            case MotionEvent.ACTION_MOVE:
                //来到新的坐标
                clickX = newX = event.getX();
                clickY = newY = event.getY();

                newPoint.x = (int) newX;
                newPoint.y = (int) newY;
                if(!isLegal()){
                    clickX = legalX;
                    clickY = legalY;
                    if(clickX == 150 || clickX == 550){
                        isDrawLR = true;
                        isDrawCircle = false;
                    }else {
                        isDrawCircle = legalDrawCircle;
                        isDrawLR = legalLR;
                    }


                }
                invalidate();
                break;
            case MotionEvent.ACTION_UP:

                break;
        }
        return true;
    }


    @Override
    public void onClick(View v) {
        clickPoint.x = (int) clickX;
        clickPoint.y = (int) clickY;
        if(isContainCircle(clickPoint, leftcirclePoint, 40)){
            clickX = leftcirclePoint.x;
            clickY = leftcirclePoint.y;
            isDrawLR = true;
            invalidate();
        }else if(isContainCircle(clickPoint, rightcirclePoint, 40)){
            clickX = rightcirclePoint.x;
            clickY = rightcirclePoint.y;
            isDrawLR = true;
            invalidate();
        }else if(rellF1.contains(clickX,clickY) && !isContainCircle(clickPoint, circlePoint, 100)
                && isContainCircle(clickPoint, circlePoint, 250)){
            isDrawCircle = true;
            invalidate();
        }
        this.listener.onProgressChanged(getTextFromX(clickX));
    }

    //根据点击的X坐标获取在圆上的Y坐标
    public float getY(float X){
        float Y;
        float middlevalue = (float) Math.sqrt(40000 - (350 - X)*(350 - X));
        Y = 250 - middlevalue;
        return Y;
    }
    //判断一个点是否在园内
    public Boolean isContainCircle(Point point,Point circle,float r){
        if(getDistance(point, circle) < r){
            return true;
        }
        return false;
    }

    //距离函数
    public float getDistance(Point point1,Point point2){
        float distance = (float) (Math.sqrt((point1.x - point2.x)*(point1.x - point2.x)
                + (point1.y - point2.y)*(point1.y - point2.y))) ;
        return distance;
    }

    public interface OnProgressChangedListener {
        void onProgressChanged(int progress);
    }
}
